package cn.stylefeng.guns.modular.User.service.Impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.enums.CommonStatusEnum;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.exception.enums.StatusExceptionEnum;
import cn.stylefeng.guns.modular.User.entity.UserOrg;
import cn.stylefeng.guns.modular.User.enums.UserEnum;
import cn.stylefeng.guns.modular.User.mapper.UserOrgMapper;
import cn.stylefeng.guns.modular.User.param.UserOrgParam;
import cn.stylefeng.guns.modular.User.service.UserOrgService;
import cn.stylefeng.guns.sys.modular.emp.service.SysEmpService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserOrgServiceImpl extends ServiceImpl<UserOrgMapper,UserOrg> implements UserOrgService {

    @Resource
    private SysEmpService sysEmpService;


    @Override
    public List<UserOrg> list(UserOrgParam userOrgParam){
        LambdaQueryWrapper<UserOrg> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ne(UserOrg::getStatus, CommonStatusEnum.DELETED.getCode());
        return this.list(queryWrapper);
    }

    @Override
    public void add(UserOrgParam userOrgParam) {
        UserOrg userOrg = new UserOrg();
        BeanUtil.copyProperties(userOrgParam,userOrg);
        this.save(userOrg);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(UserOrgParam userOrgParam) {
        UserOrg userOrg = this.queryCanteenUser(userOrgParam);


        userOrg.setStatus(CommonStatusEnum.DELETED.getCode());
        this.updateById(userOrg);
        Long id = userOrg.getId();
        //删除该用户对应的员工表信息
        sysEmpService.deleteEmpInfoByUserId(id);

        this.removeById(id);
    }



    @Override
    public void edit(UserOrgParam userOrgParam) {
        UserOrg userOrg = this.queryCanteenUser(userOrgParam);
        BeanUtil.copyProperties(userOrgParam,userOrg);
        userOrg.setStatus(null);
        this.updateById(userOrg);
    }

    @Override
    public void changeStatus(UserOrgParam userOrgParam) {
        Long id = userOrgParam.getId();
        Integer status = userOrgParam.getStatus();

        // 校验状态在不在枚举值里
        CommonStatusEnum.validateStatus(status);

        // 更新枚举，更新只能更新未删除状态的
        LambdaUpdateWrapper<UserOrg> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper
                .eq(UserOrg::getId,id)
                .and(i->i.ne(UserOrg::getStatus,CommonStatusEnum.DELETED.getCode()))
                .set(UserOrg::getStatus,status);
        boolean update = this.update(updateWrapper);
        if (!update) {
            throw new ServiceException(StatusExceptionEnum.UPDATE_STATUS_ERROR);
        }
    }
    private UserOrg queryCanteenUser(UserOrgParam userOrgParam) {
        UserOrg userOrg = this.getById(userOrgParam.getId());
        if (ObjectUtil.isEmpty(userOrg)) {
            throw new ServiceException(UserEnum.NOT_EXISTED);
        }
        return userOrg;

    }

}
